import 'package:flutter/material.dart';
import 'package:flutter_ohos/components/MTFoodItem.dart';

class MTBody extends StatefulWidget {
  const MTBody({Key? key, required this.foodList}) : super(key: key);
  final List foodList;
  @override
  _MTBodyState createState() => _MTBodyState();
}

class _MTBodyState extends State<MTBody> {
  int _activeIndex = 0;
  int getCount() {
    if (widget.foodList.length > 0) {
      return widget.foodList[_activeIndex]["foods"].length;
    }
    return 0;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white24,
      width: double.infinity,
      child: Row(
        children: [
          Container(
              color: Colors.grey,
              width: 120,
              child: Container(
                width: 120,
                child: ListView.separated(
                    itemBuilder: (BuildContext context, int index) {
                      return GestureDetector(
                        onTap: () {
                          setState(() {
                            _activeIndex = index;
                          });
                        },
                        child: Container(
                          alignment: Alignment.center,
                          height: 100,
                          color:
                              _activeIndex == index ? Colors.pink : Colors.grey,
                          child: Text(widget.foodList[index]['name']),
                        ),
                      );
                    },
                    separatorBuilder: (BuildContext context, int index) {
                      return Container(
                        width: double.infinity,
                        height: 1,
                        color: Colors.black,
                      );
                    },
                    itemCount: widget.foodList.length),
              )),
          Expanded(
              child: ListView.builder(
            itemBuilder: (BuildContext context, int index) {
              return MTFoodItem(
                  foodItem: widget.foodList[_activeIndex]['foods'][index]);
            },
            itemCount: getCount(),
          ))
        ],
      ),
    );
  }
}
